package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 用户服务
 *
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private MenuDao menuDao;
    //根据用户名查询数据库获取用户信息和关联的角色信息，同时需要查询角色关联的权限信息
    public User findByUsername(String username) {
        //单张表查询t_user where username = username
        //查询用户基本信息，不包含用户的角色
        User user = userDao.findByUsername(username);
        if(user == null){
            return null;
        }
        Integer userId = user.getId();
        //根据用户ID查询对应的角色
        //t_role t_user_role
        Set<Role> roles = roleDao.findByUserId(userId);
        if(roles != null){
            for (Role role : roles) {
                Integer roleId = role.getId();
                //角色和权限也是多对多关系
                //根据角色ID查询关联的权限
                //t_role_permission t_permission
                Set<Permission> permissions = permissionDao.findByRoleId(roleId);
                //让角色关联权限
                //根据角色的id查询数据库得到对应的权限  进行绑定
                role.setPermissions(permissions);
            }

        }
        //让用户关联角色
        user.setRoles(roles);

        return user;
    }


    //分页查询所有用户
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        Page<User> page = userDao.findPage(queryString);
        long total = page.getTotal();
        List<User> result = page.getResult();
        return new PageResult(total, result);
    }

    @Override
    public void add(User user, Integer[] roleids) {
        userDao.add(user);
        Integer id = user.getId();//获取用户id  用于建立用户和角色之间多对多关系
        if (roleids != null && roleids.length > 0) {
            for (Integer roleId : roleids) {
                Map<String, Object> map = new HashMap<>();
                map.put("userId", id);
                map.put("roleId", roleId);
                userDao.setUserAndRole(map);
            }
        }
    }

    @Override
    public User findById(Integer id) {
        User byId = userDao.findById(id);
        return byId;
    }

    @Override
    public void edit(User user, Integer[] roleIds) {
        userDao.edit(user);
        Integer id = user.getId();
        userDao.deleteUserIdAndRoleId(id);
        //
        if (roleIds != null && roleIds.length>0){
            for (Integer roleId : roleIds) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("userId",id);
                map.put("roleId",roleId);
                userDao.setUserAndRole(map);

            }
        }
    }

    @Override
    public void delete(Integer id) {
        userDao.deleteUserIdAndRoleId(id);
        userDao.deleteById(id);

    }


}